package com.pks.datacenter.controller.basic;

import io.swagger.annotations.Api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.pks.datacenter.aspect.AspectLogTag;
import com.pks.datacenter.entity.basic.DbInfo;
import com.pks.datacenter.entity.basic.JobInfo;
import com.pks.datacenter.service.basic.DbInfoService;
import com.pks.datacenter.service.basic.JobInfoService;
import com.pks.resultful.common.SRemoteResult;
import com.pks.web.dto.ServiceResult;
import com.pks.web.springmvc.controller.BasicController;

@RestController
@RequestMapping("/jobInfo")
@Scope("prototype")
@Component("jobInfoAction")
@Api(tags = "数据处理任务")
public class JobInfoAction extends BasicController {

	@Resource
	private JobInfoService jobInfoService;
	@Resource
	private DbInfoService dbInfoService;

	@RequestMapping("/tolist")
	public ModelAndView tolist(Model model) {
		return new ModelAndView("/WEB-INF/ftl/admin/basic/jobinfo_list");
	}

	@RequestMapping("/list")
	public SRemoteResult<List<JobInfo>> list(HttpServletRequest request,String name ,String nameLike ,Integer type,Integer state,Long db_source,Long db_target) {
		Map<String, Object> qbuilder = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(name)) {
			qbuilder.put("name", name);
		}
		if (StringUtils.isNotBlank(nameLike)) {
			qbuilder.put("nameLike", "%" + nameLike.trim() + "%");
		}
		if (type != null) {
			qbuilder.put("type", type);
		}
		if (state != null) {
			qbuilder.put("state", state);
		}
		if (db_source != null) {
			qbuilder.put("db_source", db_source);
		}
		if (db_target != null) {
			qbuilder.put("db_target", db_target);
		}
		List<JobInfo> list = new ArrayList<JobInfo>();
		int total = 0;
		total = jobInfoService.getTotalRows(qbuilder);
		if(total > 0 ){
			list = jobInfoService.findByPage(null, qbuilder);
			Set<Long> dbIds = new HashSet<Long>();
			for (JobInfo obj : list) {
				dbIds.add(obj.getDb_source());
				dbIds.add(obj.getDb_target());
			}
			if (dbIds.size() > 0) {
				List<DbInfo> dbInfoList = dbInfoService.find(null, dbInfoService.initQbuilder("ids", dbIds), null);
				Map<Long, DbInfo> dbInfoMap = new HashMap<Long, DbInfo>();
				for (DbInfo dbInfo : dbInfoList) {
					dbInfoMap.put(dbInfo.getId(), dbInfo);
				}
				for (JobInfo obj : list) {
					if (dbInfoMap.containsKey(obj.getDb_source())) {
						obj.setShow_db_source(dbInfoMap.get(obj.getDb_source()).getName());
					}
					if (dbInfoMap.containsKey(obj.getDb_target())) {
						obj.setShow_db_target(dbInfoMap.get(obj.getDb_target()).getName());
					}
				}
			}
		}
		return SRemoteResult.initPageList(list, total);
	}

	@RequestMapping("/toadd")
	public ModelAndView toadd(Model model) {
		List<DbInfo> dbs = dbInfoService.find("ctime desc", null, null);
		model.addAttribute("dbs", dbs);
		return new ModelAndView("/WEB-INF/ftl/admin/basic/jobinfo_modify");
	}

	@RequestMapping("/tomodify")
	public ModelAndView tomodify(Long id, Model model) {
		JobInfo obj = null;
		if(id!=null){
			obj = jobInfoService.findById(id);
		}
		model.addAttribute("obj", obj);
		List<DbInfo> dbs = dbInfoService.find("ctime desc", null, null);
		model.addAttribute("dbs", dbs);
		return new ModelAndView("/WEB-INF/ftl/admin/basic/jobinfo_modify");
	}

	@AspectLogTag(operType = "删除")
	@RequestMapping("/delete")
	public SRemoteResult<String> delete(HttpServletRequest request, Long id) {
		if (id==null) {
			return SRemoteResult.initError("请选择需要删除的记录");
		}
		try {
			jobInfoService.deleteJob(id);
			return SRemoteResult.initSuccess(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SRemoteResult.initError("操作失败");
	}
	
	@AspectLogTag(operType = "修改")
	@RequestMapping("/modify")
	public SRemoteResult<?> modify(HttpServletRequest request, JobInfo obj) {
		if (obj == null) {
			return SRemoteResult.initErrorParamMissing(null);
		}
		try {
			ServiceResult result = jobInfoService.insertOrUpdate(obj);
			return SRemoteResult.fromServiceResult(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SRemoteResult.initError("操作失败");
	}

	@RequestMapping("/findInfo")
	public SRemoteResult<Object> findInfo(HttpServletRequest request, Long id) {
		if(id==null){
			return SRemoteResult.initErrorParamMissing(null);
		}
		JobInfo obj = jobInfoService.findById(id);
		if(obj==null){
			return SRemoteResult.initErrorDataNone();
		} 
		Set<Long> dbIds = new HashSet<Long>();
		dbIds.add(obj.getDb_source());
		dbIds.add(obj.getDb_target());
		if (dbIds.size() > 0) {
			List<DbInfo> dbInfoList = dbInfoService.find(null, dbInfoService.initQbuilder("ids", dbIds), null);
			Map<Long, DbInfo> dbInfoMap = new HashMap<Long, DbInfo>();
			for (DbInfo dbInfo : dbInfoList) {
				dbInfoMap.put(dbInfo.getId(), dbInfo);
			}
			if (dbInfoMap.containsKey(obj.getDb_source())) {
				obj.setShow_db_source(dbInfoMap.get(obj.getDb_source()).getName());
			}
			if (dbInfoMap.containsKey(obj.getDb_target())) {
				obj.setShow_db_target(dbInfoMap.get(obj.getDb_target()).getName());
			}
		}
		return SRemoteResult.initSuccess(obj);
	}

}
